package com.bst.bsj.bops.web.biz.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bst.bsj.bops.web.biz.RoleService;
import com.bst.bsj.common.dal.daointerface.RoleDAO;
import com.bst.bsj.common.dal.dataobject.Role;
import com.homewaimai.common.lang.Paginator;
import com.homewaimai.common.lang.Result;
import com.homewaimai.common.lang.ResultSupport;

@Service("roleService")
public class RoleServiceImpl implements RoleService {

  private static final Log logger = LogFactory.getLog(RoleServiceImpl.class);

  @Autowired
  private RoleDAO roleDAO;

  @SuppressWarnings("rawtypes")
  @Override
  public Result selectAll(Map paramsMap) {
    Result result = new ResultSupport(false);
    List<Role> roles = roleDAO.selectAll(paramsMap);

    // 创建一个分页器(leng.intValue：每页多少条)
    Paginator paginator = new Paginator((Integer) paramsMap.get("length"));
    // 设置总项数
    Long total = roleDAO.selectAllCount(paramsMap);
    paginator.setItems(total == null ? 0 : total.intValue());
    // 设置当前页
    paginator.setPage((Integer) paramsMap.get("page"));
    result.getModels().put("paginator", paginator);
    result.getModels().put("roleList", roles);
    result.setSuccess(true);

    return result;
  }

  @Override
  public Long save(Role role) {
    Long lid = 0l;
    try {
      lid = roleDAO.save(role);
    } catch (Exception e) {
      logger.error("RoleServiceImpl.save(banner) error!", e);
    }
    return lid;
  }

  @Override
  public int delete(Long id) {
    int lid = 0;
    try {
      lid = roleDAO.delete(id);
    } catch (Exception e) {
      logger.error("RoleServiceImpl.delete(id) error!", e);
    }
    return lid;
  }

  @Override
  public int update(Role role) {
    int lid = 0;
    try {
      lid = roleDAO.update(role);
    } catch (Exception e) {
      logger.error("RoleServiceImpl.update(banner) error!", e);
    }
    return lid;
  }

  @Override
  public List<Role> selectRoles(Map<String, Object> paramsMap){
    List<Role> roles = roleDAO.selectRoles(paramsMap);
    return roles;
  }

  @Override
  public Long selectAllCount(Map<String, Object> paramsMap) {
    return roleDAO.selectAllCount(paramsMap);
  }

  @Override
  public Long selectExistCount(Map<String, Object> params) {
    return roleDAO.selectExistCount(params);
  }

}
